/*
 * LeafPage.h
 *
 *  Created on: Sep 29, 2010
 *      Author: nicoabie
 */

#ifndef LEAFPAGE_H_
#define LEAFPAGE_H_

#include <stdlib.h>
#include "Page.hpp"

template<class Key, class DataRef, class Compare = less<Key> >

class LeafPage: public Page<Key, DataRef, Compare>
{
public:
	typedef Key key_type;
	typedef Compare compare_type;

	typedef Page<Key, DataRef, Compare> page;
	typedef IndexPage<Key, DataRef, Compare> indexPage;
	typedef LeafPage<Key, DataRef, Compare> leafPage;
	typedef page* PagePtr_t;
	typedef indexPage* iPagePtr_t;
	typedef leafPage* lPagePtr_t;
	typedef AssocVector<Key, DataRef, Compare> Container;
	typedef typename Container::value_type value_type;
	typedef typename Container::iterator iterator;
	typedef typename Container::difference_type difference_type;
	typedef typename Container::size_type size_type;

protected:
	iPagePtr_t parent;
	lPagePtr_t rLink;
	lPagePtr_t lLink;
	Container entries;

public:
	LeafPage();
	LeafPage(iPagePtr_t p, lPagePtr_t rL, lPagePtr_t lL, Container e);
	LeafPage(LeafPage& x);
	template<class InputIterator>
	LeafPage(InputIterator f, InputIterator l);
	virtual ~LeafPage() {
	}
	;
	bool empty();
	size_type size();
	size_type count(const key_type& k);
	iterator find(const value_type& v);
	iterator find(const key_type& k);
	iterator begin();
	iterator end();
	iterator insert(const value_type& v);
	iterator insert(iterator pos, const value_type& v);
	template<class InputIterator>
	void insert(InputIterator f, InputIterator l);
	void erase(iterator pos);
	void erase(iterator f, iterator l);
	virtual size_type erase(const key_type& k);
	size_type max_size() const;
	iterator lower_bound(const key_type& k);
	iterator upper_bound(const key_type& k);
	std::pair<iterator, iterator> equal_range(const key_type& k);
	virtual bool isLeaf();
	virtual lPagePtr_t getLeafPage();
	virtual key_type& getPageKey();
	key_type& key(iterator i);
	virtual bool isFull();
	virtual bool isSparse();
	virtual iPagePtr_t getParent();
	virtual void setParent(iPagePtr_t iP);
	void setrLink(lPagePtr_t lP);
	void setLLink(lPagePtr_t lP);
	lPagePtr_t getrLink();
	lPagePtr_t getlLink();
	lPagePtr_t getrSibling();
	lPagePtr_t getlSibling();
	bool merge(lPagePtr_t p);
	std::pair<key_type, lPagePtr_t> split();
	std::pair<State, lPagePtr_t> borrow();

};

#endif /* LEAFPAGE_H_ */
